﻿using System;
using System.Web.Security;
using Gp.Examples.FullCalendar.Core;
using Gp.Examples.FullCalendar.Core.Repository;
using Gp.Examples.FullCalendar.Infrastructure;

namespace Gp.Examples.FullCalendar.Authentication
{
    public class LiveIdMembershipProvider : MembershipProvider
    {
        public override string ApplicationName { get; set; }

        public string ProviderName
        {
            get
            {
                return "LiveIDMembershipProvider";
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotSupportedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotSupportedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            var repository = Resolver.Resolve<IUserRepository>();

            var user = new User
            {
                Email = email,
                LiveIdHash = providerUserKey as string,
                UserName = username
            };
            repository.Save(user);

            status = MembershipCreateStatus.Success;

            return CreateMembershipUser(user);
        }

        public override bool DeleteUser(string userName, bool deleteAllRelatedData)
        {
            var repository = Resolver.Resolve<IUserRepository>();
            repository.DeleteUserByUserName(userName);

            return true;
        }

        public override bool EnablePasswordReset
        {
            get { return false; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return false; }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            var repository = Resolver.Resolve<IUserRepository>();
            var users = repository.ListUsers();

            var membershipUsers = new MembershipUserCollection();
            foreach (var user in users)
            {
                var membershipUser = CreateMembershipUser(user);
                membershipUsers.Add(membershipUser);
            }

            totalRecords = membershipUsers.Count;
            return membershipUsers;
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotSupportedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotSupportedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            var repository = Resolver.Resolve<IUserRepository>();
            var user = repository.GetUserByUserName(username);

            return CreateMembershipUser(user);
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            var repository = Resolver.Resolve<IUserRepository>();
            var user = repository.GetUserByUserName(providerUserKey.ToString());

            return CreateMembershipUser(user);
        }

        public override string GetUserNameByEmail(string email)
        {
            var repository = Resolver.Resolve<IUserRepository>();
            var user = repository.GetUserByEmail(email);
            return user.UserName;
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return 1; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 0; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 3; }
        }

        public override int PasswordAttemptWindow
        {
            get { return 5; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return MembershipPasswordFormat.Clear; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotSupportedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return true; }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotSupportedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            throw new NotImplementedException();
        }

        private MembershipUser CreateMembershipUser(User user)
        {
            if (user == null)
                return null;

            return new MembershipUser(
                        ProviderName,
                        user.UserName,
                        user.LiveIdHash,
                        string.Empty, //Email
                        string.Empty, // passwordQuestion
                        string.Empty, // comment
                        true, // isApproved
                        false, // isLockedOut
                        DateTime.MinValue, // creation date
                        DateTime.Now, // last login date
                        DateTime.Now, // lastActivityDate
                        DateTime.MinValue, // lastPasswordChangedDate
                        DateTime.MinValue // lastLockoutDate
                    );
        }
    }
}